package a66weding.com.jiehuntong.activity.me;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.AddressListBean;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MeDefaultAddressBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * 这是我的-购物地址的界面
 */
public class MeUserShippingaddressActivity extends MeBaseActivity implements View.OnClickListener, OnNetResultListener {
    private LinearLayout mBackLay;
    private TextView mBackContentTv;
    private TextView mHeaderTitleTv;
    private RelativeLayout mHeaderRightRlay;
    private Button mAddAddress;
    private Button mAddBt;
    private ListView lv;
    private ArrayList<AddressListBean.DataBean> list;
    private AddressListBean addressListBean;
    private int id;
    private LinearLayout mShowList;
    private RelativeLayout mNoList;
    private int memberId;
    private int secelt;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_user_shippingaddress);
        ((GlobalApplication) getApplication()).getActivityManager().addActivity("MeUserShippingaddressActivity", this);

        initview();
        initData();
    }


    private void initview() {
        mBackLay = (LinearLayout) findViewById(R.id.llay_header_back);
        mBackContentTv = (TextView) findViewById(R.id.tv_header_backcontent);
        mHeaderTitleTv = (TextView) findViewById(R.id.tv_header_title);
        mHeaderRightRlay = (RelativeLayout) findViewById(R.id.rlay_header_rightfunc);
        mAddAddress = (Button) findViewById(R.id.btn_me_add);
        mAddBt = (Button) findViewById(R.id.btn_add);
        lv = (ListView) findViewById(R.id.lview_me_setaddress_list);
        lv.setDividerHeight(0);
        mShowList = (LinearLayout) findViewById(R.id.rlay_me_setaddresscontent);
        mNoList = (RelativeLayout) findViewById(R.id.rlay_me_setaddressempty);

        mBackContentTv.setText("返回");
        mHeaderTitleTv.setText("我的收货地址");
        mHeaderRightRlay.setVisibility(View.GONE);

        mBackLay.setOnClickListener(this);
        mAddAddress.setOnClickListener(this);
        mAddBt.setOnClickListener(this);

    }

    //在页面可见的时候做网络请求
    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    /**
     * 页面初始化数据调用的方法
     */
    private void initData() {
        String url = UrlDealUtil.getRealUrlNewId(RequestCode.RETURN_ADDRESSISDEFAULT_1);
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                this, AddressListBean.class, RequestCode.RETURN_ADDRESSISDEFAULT_1);
        HttpHandleUtil.executeCommonPost(this, httpRequestBean);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.llay_header_back: //点击返回,关闭界面
                finish();
                break;
            case R.id.btn_me_add:   //点击添加,跳转到添加界面
                intent.setClass(MeUserShippingaddressActivity.this, MeUserAddaddressActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_add:   //点击添加,跳转到添加界面
                intent.setClass(MeUserShippingaddressActivity.this, MeUserAddaddressActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (data instanceof AddressListBean) {
            addressListBean = (AddressListBean) data;
            list = (ArrayList<AddressListBean.DataBean>) addressListBean.getData();
            if (list == null) {
                mShowList.setVisibility(View.GONE);
                mNoList.setVisibility(View.VISIBLE);
            } else {
                mShowList.setVisibility(View.VISIBLE);
                mNoList.setVisibility(View.GONE);
                myAdapter = new MyAdapter();
                lv.setAdapter(myAdapter);
            }
        } else if (data instanceof CommonResultBean) {
            CommonResultBean commonResultBean = (CommonResultBean) data;
            if ("0000".equals(commonResultBean.getResCode())){
                Toast.makeText(MeUserShippingaddressActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
            }
            initData();
        } else if(data instanceof MeDefaultAddressBean) {
            MeDefaultAddressBean meDefaultAddressBean = (MeDefaultAddressBean) data;
            if("0000".equals(meDefaultAddressBean.getResCode())){
                Toast.makeText(MeUserShippingaddressActivity.this, "默认地址设置成功", Toast.LENGTH_SHORT).show();
            }
        }

    }

    @Override
    public void onError(ErrorBean errorBean) {
        Toast.makeText(MeUserShippingaddressActivity.this, "请求数据失败", Toast.LENGTH_SHORT).show();
    }


    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            final ViewHolder mHolder;
            if (convertView != null) {
                view = convertView;
                mHolder = (ViewHolder) view.getTag();
            } else {
                view = View.inflate(getApplicationContext(), R.layout.item_me_address, null);
                mHolder = new ViewHolder();
                mHolder.mName = (TextView) view.findViewById(R.id.tv_name);
                mHolder.mPhone = (TextView) view.findViewById(R.id.tv_phone_number);
                mHolder.mAddress = (TextView) view.findViewById(R.id.tv_address);
                mHolder.mDefaultAddressTv = (TextView) view.findViewById(R.id.tv_me_address_default);
                mHolder.mDefault = (ImageView) view.findViewById(R.id.cb_me_address);
                mHolder.mCompile = (LinearLayout) view.findViewById(R.id.ll_me_compile);
                mHolder.mDelete = (LinearLayout) view.findViewById(R.id.ll_me_delete);
                view.setTag(mHolder);
            }
            mHolder.mName.setText(list.get(position).getNvc_Consignee());
            mHolder.mPhone.setText(list.get(position).getNvc_MobilePhone());
            mHolder.mAddress.setText(list.get(position).getPName() + list.get(position).getCName() +
                    list.get(position).getAName() + list.get(position).getNvc_StreetAddress());
            mHolder.mDelete.setOnClickListener(new View.OnClickListener() { //添加删除该条目的方法
                @Override
                public void onClick(View v) {
                    memberId = DataWareHourse.getInstance().getmUser().getData().get(0).getI_Id();
                    id = list.get(position).getI_Id();
                    String initUrl = UrlDealUtil.getRealUrlNoId(RequestCode.DELECT_RECEIPTADDRESS_1);
                    String url = initUrl + "&i_Id=" + id + "&memberID=" + memberId;
                    HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                            MeUserShippingaddressActivity.this, CommonResultBean.class, RequestCode.DELECT_RECEIPTADDRESS_1);
                    HttpHandleUtil.executeCommonPost(MeUserShippingaddressActivity.this, httpRequestBean);
                }
            });
            mHolder.mCompile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MeUserShippingaddressActivity.this, MeCompileAddressActivity.class);
                    intent.putExtra("mName", list.get(position).getNvc_Consignee());
                    intent.putExtra("mPhone", list.get(position).getNvc_MobilePhone());
                    intent.putExtra("mPCA", list.get(position).getPName() + list.get(position).getCName() +
                            list.get(position).getAName());
                    intent.putExtra("mAddress", list.get(position).getNvc_StreetAddress());
                    intent.putExtra("mIid", "" + list.get(position).getI_Id());
                    startActivity(intent);
                }
            });

            secelt = list.get(position).getIsDefault();
            if (secelt == 1) {
                mHolder.mDefault.setImageResource(R.mipmap.img_me_indent_accomplish);
                mHolder.mDefaultAddressTv.setTextColor(Color.rgb(243,110,93));
            } else {
                mHolder.mDefault.setImageResource(R.mipmap.img_me_no_defult_address);
                mHolder.mDefaultAddressTv.setTextColor(Color.rgb(102,102,102));
            }
            // 点击之后设置成默认收货地址
            mHolder.mDefault.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    id = list.get(position).getI_Id();
                    String initUrl = UrlDealUtil.getRealUrlNewId(RequestCode.UPDATE_ISDEFAULT_1);
                    String url = initUrl + "&addressID=" + id;
                    HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                            MeUserShippingaddressActivity.this, MeDefaultAddressBean.class, RequestCode.UPDATE_ISDEFAULT_1);
                    HttpHandleUtil.executeCommonPost(MeUserShippingaddressActivity.this, httpRequestBean);

                    for(int i = 0; i < list.size();i++){
                        if(i == position){
                            list.get(position).setIsDefault(1);
                        } else {
                            list.get(i).setIsDefault(0);
                        }
                    }
                    myAdapter.notifyDataSetChanged();
                }
            });
            return view;
        }
    }

    class ViewHolder {
        TextView mName;
        TextView mPhone;
        TextView mAddress;
        TextView mDefaultAddressTv;
        ImageView mDefault;
        LinearLayout mCompile;
        LinearLayout mDelete;
    }

}
